<template>
  <div class="box-b p10">
    <div class="w box-b">
      <lm-form :ref="editFormRefKey" inline label-width="120px" class="lm-w lm-h" :layout="layout"
        v-model="editForm">
        <template #type>
          <div class="w">
            <mark-type v-model="projectType"></mark-type>
          </div>
        </template>
      </lm-form>
      <div class="lm-w lm-h lm-f-s">
        <div class="lm-w-120 lm-h"></div>
        <el-button type="primary" @click="submit(true)">确定</el-button>
        <el-button type="danger" @click="reset" plain>重置</el-button>
        <el-button type="primary" @click="goBackToPage()" plain>返回</el-button>
      </div>
    </div>
  </div>
</template>
  
<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { type ValidateFieldsError } from 'async-validator'
import { MyTask } from '@/apis/myTask'
import { type RequestCode } from '@/server/base'
import { Project } from '@/apis/project'
import { Collection } from '@/apis/collection'
import markType from '../components/mark-type.vue'
import { Team } from '@/apis/team'

export default {
  name: 'createdMyTask',
  mixins: [frameProperty],
  props: {
    goBackToPage: Function,
    params: [Array, Object]
  } as any,
  components: { markType },
  data() {
    return {
      editFormRefKey: 'myTaskRef',
      editForm: {} as any,
      projectType: 0,

      // annotationEndTime	标注截止日期		true
      // annotationTeamId	标注团队id		false
      // clearEndTime	清洗截止日期		false
      // clearTeamId	清洗团队id		false
      // creatorId	创建者id		true
      // dataType	数据类型		false
      // doDelete	成员能否删除图片（0不可，1可以）		true
      // name	任务名称		true
      // photoListId	数据集id		true
      // reviewEndTime	审核截止日期		false
      // reviewTeamId	审核团队id		false
      // samplingRatio	抽检比例		false
      layout: [
        {
          prop: 'name',
          label: '任务名称',
          type: 'input',
          required: true,
          clearable: true,
          itemClass: 'column_2'
>>>>>>> 7088b7fdc6d03c67559950af9d15d1dc54fb6781
        },
        {
          prop: 'photoListId',
          label: '选择数据集',
          type: 'select',
          itemClass: 'column_2',
          required: true,
          clearable: true,
          filterable: true,
          disabled: this.params.type == 'all' ? false : true,
          options: []
        },
        {
          prop: 'whetherToCleanOrNot',
          label: '是否清洗',
          type: 'radio-button',
          required: true,
          clearable: true,
          itemClass: 'column_2',
          options: [
            { value: '1', label: '是' },
            { value: '0', label: '否' }
          ],
          value: '1',
          linkage(val: string, form: any, layout: any, layoutItem: any) {
            return {
              clearTeamId: {
                disabled: val != '1',
                required: val == '1'
              }
            }
          }
        },
        {
          prop: 'whetherToAudit',
          label: '是否审核',
          type: 'radio-button',
          required: true,
          clearable: true,
          itemClass: 'column_2',
          options: [
            { value: '1', label: '是' },
            { value: '0', label: '否' }
          ],
          value: '1',
          linkage(val: string, form: any, layout: any, layoutItem: any) {
            return {
              reviewTeamId: {
                disabled: val != '1',
                required: val == '1'
              }
            }
          }
        },

        {
          prop: 'clearTeamId',
          label: '清洗团队',
          type: 'select',
          required: true,
          clearable: true,
          filterable: true,
          itemClass: 'column_2',
          options: [
            // { value: '1', label: '是' },
            // { value: '0', label: '否' },
          ]
        },
        {
          prop: 'assignADataType',
          label: '分配数据类型',
          type: 'select',
          required: true,
          disabled: true,
          filterable: true,
          itemClass: 'column_2',
          options: [
            { value: '1', label: '未标注' },
            { value: '0', label: '已标注' }
          ],
          value: '1'
        },
        {
          prop: 'annotationTeamId',
          label: '标注团队',
          type: 'select',
          required: true,
          clearable: true,
          filterable: true,
          itemClass: 'column_2',
          options: [],
          linkage(val: string, layoutItem: any, form: any, formLayout: any) {
            const findIndex = formLayout.findIndex(
              (item: { prop: string }) => item.prop == 'photoListId'
            )
            if (findIndex < 0) {
              return
            }
            MyTask.markNum({ photoListId: formLayout[findIndex].value, annTeamId: val }).then(
              (res: RequestCode) => {
                //只需修改form表单的值即可
                form.annotationNumber = res.data || 0
              }
            )
          }
        },
        {
          prop: 'annotationNumber',
          label: '每人标注数量',
          type: 'input',
          itemClass: 'column_2',
          required: true,
          clearable: true,
          disabled: true,
          //   value: '',
          append: '&nbsp;个'
        },
        {
          prop: 'annotationEndTime',
          label: '任务截止时间',
          itemClass: 'column_2',
          type: 'date',
          required: true,
          clearable: true
        },
        {
          prop: 'doDelete',
          label: '成员权限范围',
          itemClass: 'column_2',
          type: 'checkbox',
          clearable: true,
          options: [
            { value: 'delete_image', label: '删除图片' },
            { value: 'red_annotate', label: '查看标注' }
          ]
        },

        {
          prop: 'reviewTeamId',
          label: '审核团队',
          type: 'select',
          required: true,
          clearable: true,
          filterable: true,
          itemClass: 'column_2',
          options: []
        },

        {
          prop: 'samplingRatio',
          label: '抽检比例',
          type: 'input',
          regex: /^[0-9]*[.]?[0-9]*$/,
          itemClass: 'column_2',
          required: true,
          clearable: true,
          append: '&nbsp;%'
        }
      ] as any[]
    }
  },
  watch: {
    layout(val) {
      console.log(val)
    }
  },
  methods: {
    annotationTeamIdChange() {
      console.log('222')
    },
    getTeamList() {
      Team.list()
        .then((res: RequestCode) => {
          this.setTeamToEditFormLayout(res.data)
        })
        .catch(() => {})
    },
    setTeamToEditFormLayout(
      data: {
        id: number | string
        name: string
        type: number | string
        explain: string
      }[]
    ) {
      const keys = ['reviewTeamId', 'annotationTeamId', 'clearTeamId']
      keys.map((key: string) => {
        const findIndex = this.layout.findIndex((item: { prop: string }) => item.prop == key)
        if (findIndex < 0) {
          return
        }
        this.layout[findIndex].options = data.map(
          (li: { id: number | string; name: string; type: number | string; explain: string }) => {
            return {
              label: li.name,
              value: li.id
            }
          }
        )
      })
    },
    /**
     * 查询数据组
     */
    imageCollection() {
      const findIndex = this.layout.findIndex(
        (item: { prop: string }) => item.prop == 'photoListId'
      )
      if (findIndex < 0) {
        return
      }
      Collection.imageCollection('', '')
        .then((res: RequestCode) => {
          this.layout[findIndex].options = res.data.map((item: any) => {
            return { label: item.image[0].photo.name, value: item.image[0].photo.id + '' }
          })
        })
        .catch(() => {})
    },

    saveInfo(isContinue: boolean) {
      // const findIndex = this.layout.findIndex((item: { prop: string }) => item.prop == 'projectId')
      // const options = this.layout[findIndex].options as any[]
      let params = this.editForm as any
      params.creatorId = this.userInfo.id
      params.doDelete =
        !params.doDelete ||
        params.doDelete.findIndex((item: string[]) => item.includes('delete_image')) < 0
          ? 0
          : 1
      // params.projectName = options.filter(
      //     (item: { value: string | number; label: string }) => item.value == params.projectId
      // )?.[0]?.label

      MyTask.saveMyTask(params).then((res: RequestCode) => {
        this.$message({ type: 'success', message: '操作完成！' })
        if (!isContinue) {
          return
        }
        this.goBackToPage(true)
        // const isEdit = !this.editForm.id ? this.urlPathCode.FALSE : this.urlPathCode.TRUE
        // this.$router.push({
        //     name: 'importMyTask',
        //     params: { isEdit, value: this.getParamsString(this.editForm) }
        // })
      })
    },
    //验证数据
    submit(isContinue: boolean) {
      const editFormRef = this.$refs[this.editFormRefKey] as any
      editFormRef.validate((isValid: boolean, invalidFields?: ValidateFieldsError) => {
        if (!isValid) {
          //滚动到未通过校验的字段
          if (invalidFields && Object.keys(invalidFields).length > 0) {
            editFormRef.scrollToField(Object.keys(invalidFields)[0])
          }
          return
        }
        this.saveInfo(isContinue)
      })
    },
    //重置
    reset() {
      this.editForm = {}
    }
  },
  created() {
    if (this.params.type == 'all') {
      this.imageCollection()
    } else {
      const findIndex = this.layout.findIndex(
        (item: { prop: string }) => item.prop == 'photoListId'
      )
      if (findIndex < 0) {
        return
      }
      this.layout[findIndex].options = []
      this.layout[findIndex].options.push({
        label: this.params.projectName,
        value: this.params.projectId
      })
      this.layout[findIndex].value = this.layout[findIndex].options[0].value
    }
    this.getTeamList()
  }
}
</script>
  
<style lang="scss" scoped></style>